"""post模型"""
from datetime import datetime
from enum import Enum
from typing_extensions import Annotated, List, Union

from fastapi import Body, Path, Query
from pydantic import BaseModel, ConfigDict, model_validator, Field

from api.v1.models.pagination import PaginationResponse, PagenationRequest


class WriteModel(BaseModel):  #这个和models.py文件中的writebasemodel 不是一回事，
    """留言板个别文章模式"""

    # 允许添加字段
    model_config = ConfigDict(extra='allow')   #允许添加字段

    wr_subject: Annotated[str, Body(..., max_length=255, title="标题")]
    wr_content: Annotated[str, Body("", title="内容")]  #内容
    wr_name: Annotated[str, Body("", max_length=255, title="作者", 
                                 description="非会员的作者姓名")]
    wr_password: Annotated[str, Body("", max_length=255, title="密码",
                                     description="如果您是非会员，请输入密码")]
    wr_email: Annotated[str, Body("", max_length=255, title="电子邮件")]
    wr_homepage: Annotated[str, Body("", max_length=255, title="主页")]
    wr_link1: Annotated[str, Body("", title="链接1")]
    wr_link2: Annotated[str, Body("", title="链接2")]
    wr_option: Annotated[str, Body("", title="选项")]
    html: Annotated[str, Body("", title="HTML 是否")]
    mail: Annotated[str, Body("", title="是否铣削")]
    secret: Annotated[str, Body("", title="秘密写作与否")]
    ca_name: Annotated[str, Body("", title="类别")]
    notice: Annotated[bool, Body(False, title="是否发布公告")]
    parent_id: Annotated[int, Body(None, title="父文章ID")]

    @model_validator(mode='after')#这个装饰器的主要用途是在模型实例创建后对未声明的字段进行初始化或其它处理。在此示例中，它用于为 wr_datetime 字段赋值，
    #以确保每当创建 WriteModel 实例时，都能记录当前的时间信息。这种方式可以在模型的生命周期中确保某些字段始终具有有效的数据。
    def init_fields(self) -> 'WriteModel':
        """WriteModel初始化一个未声明的字段"""  #初始化中未声明的字段
        self.wr_datetime: datetime = datetime.now()
        return self


class CommentModel(BaseModel):
# model_config = ConfigDict(extra='allow')

# wr_content:Annotated[str，Body（…，title=“内容”）
# wr_name:Annotated[str，Body（“”，title=“作者”），
# description=“如果是非会员，则作者姓名”）
# wr_password:Annotated[str，Body（“”，title=“密码”），
# description=“非会员密码”）
# wr_option:Annotated[str，Body（“html1”，title=“是否有秘密文章”），
# description=“secret:秘密文章，html1:使用HTML”）
# comment_id:Annotated[int，Body（None，title=“评论ID”）

# @model_validator(mode='after')
# def init_fields(self) -> 'WriteModel':
# “初始化CommentModel中未声明的字段”
# self.wr_is_comment: int = 1
# return self


# 允许添加字段
    model_config = ConfigDict(extra='allow')

    wr_content: Annotated[str, Body(..., title="内容")]
    wr_name: Annotated[str, Body("", title="按作者",
                                  description="如果您是非会员，作者姓名")]
    wr_password: Annotated[str, Body("", title="密码",
                                     description="如果您是非会员，请输入密码")]
    wr_option: Annotated[str, Body("html1", title="秘密写作与否",
                                   description="secret: 秘密写作, html1: HTML 使用")]
    comment_id: Annotated[int, Body(None, title="评论 ID")]

    @model_validator(mode='after')
    def init_fields(self) -> 'WriteModel':
        """在CommentModel中初始化未声明的字段"""
        self.wr_is_comment: int = 1
        return self


class CommentUpdateModel(BaseModel):
    """公告板评论修正模型"""

    # 允许其他字段
    model_config = ConfigDict(extra='allow')

    wr_content: Annotated[str, Body(..., title="内容")]
    wr_password: Annotated[str, Body("", title="密码",
                                     description="如果您是非会员，请输入密码")]
    wr_option: Annotated[str, Body("html1", title="秘密写作与否",
                                   description="secret: 秘密写作, html1: HTML 使用")]
    comment_id: Annotated[int, Body(None, title="评论 ID")]


class ResponseNormalModel(BaseModel):
    result: str


class ResponseCreateWriteModel(ResponseNormalModel):
    wr_id: int


class ResponseFileModel(BaseModel):
    """在帖子文件模型中 response中所需的属性定义"""
    bf_source: str
    bf_filesize: int
    bf_download: int
    bf_datetime: datetime
    bf_file: str


class ResponseCommentModel(BaseModel):
    """在模型之间发表评论 response中所需的属性定义"""
    wr_id: int
    wr_parent: int
    wr_name: str
    mb_id: str
    mb_image_path: str
    mb_icon_path: str
    save_content: str
    wr_datetime: datetime
    wr_last: str
    wr_option: str
    wr_email: str
    wr_comment: int
    wr_comment_reply: str
    is_reply: bool
    is_edit: bool
    is_del: bool
    is_secret: bool
    is_secret_content: bool


class ResponseWriteSearchModel(BaseModel):
    """在邮政模型中 response中所需的属性定义"""
    wr_id: int
    wr_num: int
    wr_reply: str
    wr_subject: str
    wr_name: str
    wr_datetime: datetime
    wr_content: str
    wr_comment: int
    wr_hit: int
    wr_option: str = "",

    class Config:
        from_attributes = True


class ThumbnailModel(BaseModel):
    """要在post模型中使用的缩略图模型"""
    src: str
    alt: str
    noimg: str


class ResponseWriteModel(BaseModel):
    """定义帖子模型中response所需的属性"""
    wr_id: int
    wr_num: int
    wr_reply: str
    wr_subject: str
    wr_name: str
    mb_id: str
    mb_image_path: str
    mb_icon_path: str
    wr_datetime: datetime
    wr_email: str
    wr_content: str
    wr_link1: str
    wr_link2: str
    wr_comment: int
    wr_hit: int
    wr_ip: str
    good: int
    nogood: int
    thumbnail: ThumbnailModel
    wr_option: str = "",
    images: List[ResponseFileModel] = []
    normal_files: List[ResponseFileModel] = []
    comments: List[ResponseCommentModel] = []

    class Config:
        from_attributes = True


class ResponseBoardModel(BaseModel):
    """
    在公告板模型中 response中所需的属性定义
    它还包括一个过滤功能，该功能可删除未使用类型验证定义的属性
    通过展开或添加带壳部分 API response您可以更改中显示的数据.
    """
    bo_table: str
    gr_id: str
    bo_subject: str
    bo_mobile_subject: str
    bo_device: str
    bo_admin: str
    bo_list_level: int
    bo_read_level: int
    bo_write_level: int
    bo_reply_level: int
    bo_comment_level: int
    bo_upload_level: int
    bo_download_level: int
    bo_html_level: int
    bo_link_level: int
    bo_count_delete: int
    bo_count_modify: int
    bo_read_point: int
    bo_write_point: int
    bo_comment_point: int
    bo_download_point: int
    bo_use_category: int
    bo_category_list: str
    # bo_use_sideview: int
    # bo_use_file_content: int
    # bo_use_secret: int
    # bo_use_dhtml_editor: int
    # bo_select_editor: str
    # bo_use_rss_view: int
    # bo_use_good: int
    # bo_use_nogood: int
    # bo_use_name: int
    # bo_use_signature: int
    # bo_use_ip_view: int
    # bo_use_list_view: int
    # bo_use_list_file: int
    # bo_use_list_content: int
    # bo_table_width: int
    # bo_subject_len: int
    # bo_mobile_subject_len: int
    # bo_page_rows: int
    # bo_mobile_page_rows: int
    # bo_new: int
    # bo_hot: int
    # bo_image_width: int
    # bo_skin: str
    # bo_mobile_skin: str
    # bo_include_head: str
    # bo_include_tail: str
    # bo_content_head: str
    # bo_mobile_content_head: str
    # bo_content_tail: str
    # bo_mobile_content_tail: str
    # bo_insert_content: str
    # bo_gallery_cols: int
    # bo_gallery_width: int
    # bo_gallery_height: int
    # bo_mobile_gallery_width: int
    # bo_mobile_gallery_height: int
    # bo_upload_size: int
    # bo_reply_order: int
    # bo_use_search: int
    # bo_order: int
    # bo_count_write: int
    # bo_count_comment: int
    # bo_write_min: int
    # bo_write_max: int
    # bo_comment_min: int
    # bo_comment_max: int
    # bo_notice: str
    # bo_upload_count: int
    # bo_use_email: int
    # bo_use_cert: str
    # bo_use_sns: int
    # bo_use_captcha: int
    # bo_sort_field: str
    # bo_1_subj: str
    # bo_2_subj: str
    # bo_3_subj: str
    # bo_4_subj: str
    # bo_5_subj: str
    # bo_6_subj: str
    # bo_7_subj: str
    # bo_8_subj: str
    # bo_9_subj: str
    # bo_10_subj: str
    # bo_1: str
    # bo_2: str
    # bo_3: str
    # bo_4: str
    # bo_5: str
    # bo_6: str
    # bo_7: str
    # bo_8: str
    # bo_9: str
    # bo_10: str


class BoardPaginationRequest(PagenationRequest):
    per_page: int = Field(
        Query(default=0,
              ge=0,
              le=100,
              title="输出的数量",
              description="""每页的结果数 <br>\
                最大值 100 <br>\
                0并按照个人公告板设置。 <br>\
                个人公告板设置 0默认设置的值。
              """
              )
    )


class ResponseBoardListModel(PaginationResponse):
    """公告板列表模型"""
    categories: list
    board: ResponseBoardModel
    notice_writes: List[ResponseWriteModel]
    writes: List[ResponseWriteModel]
    total_count: int
    current_page: int
    prev_spt: Union[int, None]
    next_spt: Union[int, None]


class ResponseTotalBoardNewListModel(BaseModel):
    """最新写作列表模型"""
    free: List[ResponseWriteModel]
    gallery: List[ResponseWriteModel]
    qa: List[ResponseWriteModel]
    notice: List[ResponseWriteModel]


class ResponseGroupModel(BaseModel):
    """公告板组模型"""
    gr_id: str
    gr_subject: str
    gr_device: str
    gr_admin: str
    gr_use_access: int
    gr_order: int


class ResponseGroupBoardsModel(BaseModel):
    """公告板组, 게시판 목록 모델"""
    """게시판 그룹, 公告板列表模型"""
    group: ResponseGroupModel
    boards: List[ResponseBoardModel]


class BoardNewViewType(str, Enum):
    WRITE = "write"
    COMMENT = "comment"


class RequestBoardNewWrites(BaseModel):
    """最新文章查询 API 请求模型"""

    view_type: BoardNewViewType = Field(Query(
        default=BoardNewViewType.WRITE,
        title="查询类型",
        description="post/评论"
    ))
    rows: int = Field(Query(
        default=10,
        title="要输出的最新文章数",
        description="要输出的最新文章数"
    ))


class ResponseBoardNewModel(BaseModel):
    """最新文章型号"""
    bo_table: str
    wr_id: int
    wr_parent: int
    bn_datetime: datetime
    mb_id: str
    num: int


class ResponseBoardNewListModel(PaginationResponse):
    """最新写作列表模型"""
    board_news: List[ResponseBoardNewModel]
    current_page: int


class ResponseSearchBoardModel(ResponseBoardModel):
    """搜索结果公告板模型"""
    writes: List[ResponseWriteSearchModel]


class ResponseSearchModel(PaginationResponse):
    """搜索结果模型"""
    onetable: Union[str, None]
    boards: List[ResponseSearchBoardModel]


class WriteTransportation(Enum):
    """移动帖子/复制"""
    MOVE = 'move'
    COPY = 'copy'


class WriteTransportationRequest(BaseModel):
    """移动帖子/复制 API 请求模型"""
    sw: WriteTransportation = Field(Path(title="移动/复制", description="移动或复制帖子"))
